/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package model;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Set;

/**
 *
 * @author xgzheng
 */
public class Product implements TableInterface, Compareable {

    /** 主键 */
    private int pId;
    /** 产品名称 */
    private String pName;
//    /** 颜色||色号 */
//    private String pColor;
    /** 产品价格 */
    private double pPrice;
    /** 产品库存 */
    private double pStorage;
    /** 产品图片 */
    private String pImg;
    /** 厂家 */
    private Vendor vendor;
    /** sellItem */
    private Set<SellItem> sellItem;
    private boolean exist;

    public int getpId() {
        return pId;
    }

    public void setpId(int pId) {
        this.pId = pId;
    }

    public String getpImg() {
        return pImg;
    }

    public void setpImg(String pImg) {
        this.pImg = pImg;
    }

    public String getpName() {
        return pName;
    }

    public void setpName(String pName) {
        this.pName = pName;
    }

    public double getpPrice() {
        return pPrice;
    }

    public void setpPrice(double pPrice) {
        this.pPrice = pPrice;
    }

    public double getpStorage() {
        return pStorage;
    }

    public void setpStorage(double pStorage) {
        this.pStorage = pStorage;
    }

    public Set<SellItem> getSellItem() {
        return sellItem;
    }

    public void setSellItem(Set<SellItem> sellItem) {
        this.sellItem = sellItem;
    }

    public Vendor getVendor() {
        return vendor;
    }

    public void setVendor(Vendor vendor) {
        this.vendor = vendor;
    }

//    /**
//     * @return the pColor
//     */
//    public String getpColor() {
//        return pColor;
//    }
//
//    /**
//     * @param pColor the pColor to set
//     */
//    public void setpColor(String pColor) {
//        this.pColor = pColor;
//    }
    public void insert(PreparedStatement pstmt) throws SQLException {
        pstmt.setInt(1, pId);
        if(pName == null) {
            pName = "--";
        }
        pstmt.setString(2, pName);
        pstmt.setDouble(3, pPrice);
        pstmt.setDouble(4, pStorage);
        pstmt.setString(5, pImg);
        if (vendor != null) {
            pstmt.setInt(6, vendor.getvId());
        } else {
            vendor = new Vendor();
            vendor.setvId(-1);
            pstmt.setInt(6, vendor.getvId());
        }
        pstmt.executeUpdate();
    }

    public String getTableName() {
        return "product";
    }

    public int getTableColumnCount() {
        return 6;
    }

    public String getPrimaryKey() {
        return "pid";
    }

    public Object getObject(ResultSet resultSet) throws SQLException {
        setExist(false);
        if (resultSet.next()) {
            setpId(resultSet.getInt("pid"));
            setpName(resultSet.getString("pname"));
            setpPrice(resultSet.getDouble("pprice"));
            setpStorage(resultSet.getDouble("pstorage"));
            setpImg(resultSet.getString("pimg"));
            setExist(true);
//            p.setVendor(vendor);
        }
        return this;
    }

    public Object compareObject(Object obj, ResultSet resultSet) throws SQLException {
        setExist(false);
        Product product = (Product) obj;
        if (resultSet.next()) {
            setpId(resultSet.getInt("pid"));
            setpName(resultSet.getString("pname"));
            setpPrice(resultSet.getDouble("pprice"));
            setpStorage(resultSet.getDouble("pstorage"));
            setpImg(resultSet.getString("pimg"));
            if (this.pName.equals(product.getpName()) && this.getpPrice() == product.getpPrice()) {
                setExist(true);
                return this;
            }
        }
        return product;
    }

    /**
     * @return the exist
     */
    public boolean isExist() {
        return exist;
    }

    /**
     * @param exist the exist to set
     */
    public void setExist(boolean exist) {
        this.exist = exist;
    }
}
